package com.buanalintas.sandals.pos.finance;

import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.entities.MasterUnit;
import com.buanalintas.bynga.formatter.DateFormatter;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosTransactionFinance;
import com.buanalintas.sandals.pos.entities.PosTransactionSale;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author dedy
 */
public class Finance extends Page {

    private Field financeId;
    private Field financeUnitId;
    private Field financeFromDate;
    private Field financeToDate;
    private Field financeTotalSale;
    private Field financeExpense;
    private Field financeNetSale;
    private Field financeGrossProfit;
    private Field financeCost;
    private Field financeNett;
    private Field financeTimestamp;

    public Finance() {
        super();
        setTheClass(PosTransactionFinance.class);

        financeId = new Field();
        financeId.setName("financeId");
        financeId.setCaption("id");
        financeId.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        financeId.setInputProperties(" size=\"20\"");
        getFields().add(financeId);

        financeUnitId = new Field();
        financeUnitId.setName("financeUnitId");
        financeUnitId.setCaption("unit");
        financeUnitId.setListProperties(" width=\"20%\"");
        financeUnitId.setInputProperties(" size=\"15\" readonly");
        financeUnitId.setRequired(true);
        financeUnitId.getSelectorColumns().add(new String[]{"unitName", " Name", "width=\"30%\" nowrap"});
        financeUnitId.getSelectorColumns().add(new String[]{"unitDescription", " Description", "width=\"70%\""});
        financeUnitId.getViewColumns().addAll(financeUnitId.getSelectorColumns());
        getFields().add(financeUnitId);

        financeFromDate = new Field();
        financeFromDate.setName("financeFromDate");
        financeFromDate.setCaption("from date");
        financeFromDate.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        financeFromDate.setInputProperties(" size=\"8\"");
        financeFromDate.setRequired(true);
        financeFromDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(financeFromDate);

        financeToDate = new Field();
        financeToDate.setName("financeToDate");
        financeToDate.setCaption("to date");
        financeToDate.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        financeToDate.setInputProperties(" size=\"8\"");
        financeToDate.setRequired(true);
        financeToDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(financeToDate);

        financeTotalSale = new Field();
        financeTotalSale.setName("financeTotalSale");
        financeTotalSale.setCaption("total sale");
        financeTotalSale.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        financeTotalSale.setInputProperties(" size=\"8\" readonly");
        financeTotalSale.setRequired(true);
        financeTotalSale.setFormater(MoneyFormatter.getInstance());
        getFields().add(financeTotalSale);

        financeExpense = new Field();
        financeExpense.setName("financeExpense");
        financeExpense.setCaption("Expense");
        financeExpense.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        financeExpense.setInputProperties(" size=\"8\"");
        financeExpense.setRequired(true);
        financeExpense.setFormater(MoneyFormatter.getInstance());
        getFields().add(financeExpense);

        financeNetSale = new Field();
        financeNetSale.setName("financeNetSale");
        financeNetSale.setCaption("net sale");
        financeNetSale.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        financeNetSale.setInputProperties(" size=\"8\"");
        financeNetSale.setFormater(MoneyFormatter.getInstance());
        getFields().add(financeNetSale);

        financeGrossProfit = new Field();
        financeGrossProfit.setName("financeGrossProfit");
        financeGrossProfit.setCaption("gross profit");
        financeGrossProfit.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        financeGrossProfit.setInputProperties(" size=\"8\"");
        financeGrossProfit.setFormater(MoneyFormatter.getInstance());
        getFields().add(financeGrossProfit);

        financeCost = new Field();
        financeCost.setName("financeCost");
        financeCost.setCaption("cost");
        financeCost.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        financeCost.setInputProperties(" size=\"8\"");
        financeCost.setRequired(true);
        financeCost.setFormater(MoneyFormatter.getInstance());
        getFields().add(financeCost);

        financeNett = new Field();
        financeNett.setName("financeNett");
        financeNett.setCaption("Nett");
        financeNett.setListProperties(" align=\"right\" width=\"20%\" nowrap");
        financeNett.setInputProperties(" size=\"8\"");
        financeNett.setFormater(MoneyFormatter.getInstance());
        getFields().add(financeNett);

        financeTimestamp = new Field();
        financeTimestamp.setName("financeTimestamp");
        financeTimestamp.setCaption("timestamp");
        financeTimestamp.setListProperties(" align=\"center\" width=\"15%\" nowrap");
        financeTimestamp.setInputProperties(" size=\"8\" readonly");
        financeTimestamp.setDefaultValue(Utility.getTimestamp());
        getFields().add(financeTimestamp);


        fillFields();

        getFieldsAdd().remove(financeId);
        getFieldsAdd().remove(financeNetSale);
        getFieldsAdd().remove(financeGrossProfit);
        getFieldsAdd().remove(financeTotalSale);
        getFieldsAdd().remove(financeTimestamp);
        getFieldsAdd().remove(financeNett);
        getFieldsEdit().remove(financeId);
        getFieldsList().remove(financeId);
        getFieldsPrint().remove(financeId);
        getFieldsPrintList().remove(financeId);
        getFieldsView().remove(financeId);

        getFieldsSearch().remove(financeId);

        setController(new FinanceController(this));
        setExportList(getClass().getResourceAsStream("FinanceList.jasper"));
        setExport(getClass().getResourceAsStream("Finance.jasper"));
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Finance"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();


    }

    @Override
    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
        financeTimestamp.setFormater(DateFormatter.getInstanceDate(this));
    }

    class FinanceController extends Controller {

        public FinanceController(Page page) {
            super(page);
        }

        @Override
        public void beforeInsert(Object data) {
            PosTransactionFinance finance = (PosTransactionFinance) data;
            EntityManager em = getEntityManager();

            MasterUnit unitId = finance.getFinanceUnitId();

            Date start = finance.getFinanceFromDate();
            Date end = finance.getFinanceToDate();
//            System.out.println("======================>>>" + start + "====" + end );
            Query query = em.createQuery("SELECT t FROM PosTransactionSale t WHERE (t.saleDate BETWEEN :start AND :end) AND (t.saleUnitId =:unit)");
//            Query query = em.createQuery("SELECT t FROM PosTransactionSale t WHERE t.saleUnitId =:unit");

            query.setParameter("unit", unitId);
            query.setParameter("start", start);
            query.setParameter("end", end);

            BigDecimal saleTotal = new BigDecimal(0);
            BigDecimal nettSale = new BigDecimal(0);
            BigDecimal grossProfit = new BigDecimal(0);
            BigDecimal nett = new BigDecimal(0);
            BigDecimal percent = new BigDecimal(0.4);
            try {
                List<PosTransactionSale> sale = query.getResultList();
                for (int i = 0; i < sale.size(); i++) {
                    System.out.println("======================>>>" + sale.get(i).getSaleTotal());

                    saleTotal = saleTotal.add(sale.get(i).getSaleTotal());
                }
            } catch (Exception e) {
            }
            try {
                nettSale = saleTotal.subtract(finance.getFinanceExpense());
                System.out.println("======================>>>" + nettSale);

                grossProfit = nettSale.multiply(percent);
                System.out.println("======================>>>" + nett);

                nett = grossProfit.subtract(finance.getFinanceCost());
                System.out.println("======================>>>" + 444);
            } catch (Exception e) {
            }
            System.out.println("======================>>>" + nettSale + "====" + grossProfit + "====" + nett);
            finance.setFinanceTotalSale(saleTotal);
            finance.setFinanceNetSale(nettSale);
            finance.setFinanceGrossProfit(grossProfit);
            finance.setFinanceNett(nett);
            finance.setFinanceTimestamp(Utility.getTimestamp());

        }

        @Override
        public void afterInsert(Object data) {
            super.afterInsert(data);
        }
    }
}
